/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agent;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.InstanceAlreadyExistsException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

/**
 *
 * @author alan
 */
public class AgentMBean {

    private static JMXServiceURL JMXURL;
    private static JMXConnector JMXConn;
    private static MBeanServerConnection JMXSC;

    public AgentMBean() {
    }

    public static MBeanServerConnection makeAgent(String ip) {
        try {
            JMXURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + ip + ":17503/server");
            JMXConn = JMXConnectorFactory.connect(JMXURL, null);
            JMXSC = JMXConn.getMBeanServerConnection();
            return JMXSC;
        } catch (IOException ex) {
            return null;
        }
    }

    public static void main(String[] args) throws MalformedObjectNameException {
        try {
            JMXSC = AgentMBean.makeAgent("localhost");
            ObjectName mbean;
            mbean = new ObjectName("tcc:type=MACHINE");
            if (!JMXSC.isRegistered(mbean)) {
                JMXSC.createMBean("agent.AgentWindows", mbean);

            }
            MBeanInfo mbeanInfo = JMXSC.getMBeanInfo(mbean);
            for (MBeanAttributeInfo m : mbeanInfo.getAttributes()) {
                System.out.println(m.getName());
            }
        } catch (InstanceAlreadyExistsException ex) {
            Logger.getLogger(AgentMBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MBeanRegistrationException ex) {
            Logger.getLogger(AgentMBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotCompliantMBeanException ex) {
            Logger.getLogger(AgentMBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IntrospectionException ex) {
            Logger.getLogger(AgentMBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MBeanException ex) {
            Logger.getLogger(AgentMBean.class.getName()).log(Level.SEVERE, null, ex);

            Logger.getLogger(AgentMBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstanceNotFoundException ex) {
            Logger.getLogger(AgentMBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ReflectionException ex) {
            Logger.getLogger(AgentMBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(AgentMBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
